import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { GamerBackPack } from '../GamerBackpack';
import { Ammo } from '../Items/Ammo';
import { Armor } from '../Items/Armor';
import { Gun } from '../Items/Gun';
import { Heal } from '../Items/Heal';
import { Item } from '../Items/Item';
import { Melee } from '../Items/Melee';
import { Module } from '../Items/Module';
import { Pack } from '../Items/Pack';
import { PlayerController } from '../PlayerController';
import { InGameManager } from '../Managers/InGameManager';
import { BundleManager } from 'db://assets/Scripts/Framework/Managers/BundleManager';
import { Adorn } from '../Items/Adorn';
import { GameDataManager } from '../GameDataManager';
const { ccclass, property } = _decorator;

@ccclass('BoxItemUI')
export class BoxItemUI extends Component {

    @property(Sprite)
    bgSprite: Sprite;

    @property(Sprite)
    bg2Sprite: Sprite;

    @property(Sprite)
    sprite: Sprite;

    @property(Sprite)
    dulSprite: Sprite;

    @property(Label)
    nameLabel: Label;

    @property([SpriteFrame])
    bgSfs: SpriteFrame[] = [];
    private isSuggest = false;
    public get IsSuggest(): boolean {
        return this.isSuggest;
    }
    public set IsSuggest(value: boolean) {
        this.isSuggest = value;
        this.bgSprite.spriteFrame = this.bgSfs[this.isSuggest ? 2 : 0];
        this.bg2Sprite.spriteFrame = this.bgSfs[this.isSuggest ? 3 : 1];
    }

    item: Item;
    pack: GamerBackPack;

    onLoad() {

    }

    update(deltaTime: number) {

    }

    Init(_item: Item) {
        this.pack = PlayerController.Instance.backPack;
        this.item = _item;
        BundleManager.GetBundle("MainPack").load(_item.sfUrl, SpriteFrame, (err, data) => { this.sprite.spriteFrame = data; });
        this.nameLabel.string = _item.itemName;
        if (_item instanceof Heal || _item instanceof Ammo) this.nameLabel.string += `(${_item.num})`;
        this.Check();
    }

    Check() {
        this.dulSprite.fillRange = 0;
        if (this.item.lastOwner == this.pack) return this.IsSuggest = false;
        if (this.item instanceof Gun) {
            let guns = this.pack.guns;
            if (!guns || (!guns[0] && this.item.type == 0)) return this.IsSuggest = true;
            for (let i = 1; i < guns.length; i++) {
                const element = guns[i];
                if (!element) return this.IsSuggest = true;
            }
        }
        else if (this.item instanceof Module) {
            let guns = this.pack.guns;
            if (!guns) return this.IsSuggest = false;
            for (let i = 0; i < guns.length; i++) {
                const element = guns[i];
                if (element && element.accessModules[this.item.type] && (!element.modules[this.item.type] || element.modules[this.item.type].figure < this.item.figure)) return this.IsSuggest = true;
            }
        }
        else if (this.item instanceof Melee) {
            if (!this.pack.melee) return this.IsSuggest = true;
        }
        else if (this.item instanceof Armor) {
            this.dulSprite.fillRange = 1 - this.item.durability / this.item.maxDurability;
            let armors = this.pack.armors;
            if (armors.length == 0) return this.IsSuggest = true;
            let armor = this.pack.armors[this.item.type];
            if (!armor || armor.durability == 0 || armor.level < this.item.level || armor.level == this.item.level && armor.durability < this.item.durability) return this.IsSuggest = true;
        }
        else if (this.item instanceof Heal) {
            let heal = this.pack.items.find((value, index, obj) => { if (value.itemName == this.item.itemName) return value }) as Heal;
            if (!heal) return this.IsSuggest = true;
        }
        else if (this.item instanceof Ammo) {
            let guns = this.pack.guns;
            if (!guns) return this.IsSuggest = false;
            let types: number[] = [];
            guns.forEach(element => { if (element) types.push(element.type); });
            for (let i = 0; i < types.length; i++) {
                const element = types[i];
                if (element == this.item.type) return this.IsSuggest = true;
            }
        }
        else if (this.item instanceof Pack) {
            if (!this.pack.pack || this.pack.pack.level < this.item.level) return this.IsSuggest = true;
        }
        else if (this.item instanceof Adorn) return this.IsSuggest = true;
        return this.IsSuggest = false;
    }

    Take() {
        this.pack.GetItem(this.item);
    }

}


